package com.oa.hyperion.repository;

import com.oa.hyperion.entity.Attendance;
import com.oa.hyperion.entity.Detail;
import com.oa.hyperion.utils.StringIsEmpty;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Tang
 */
public interface AttendanceMapper {
    @Select("select * from attendance where employee=#{employee} and DATE_FORMAT(create_date,'%Y-%m-%d')=#{createDate}")
    public Attendance findAttendance(@Param("employee")Integer employee,@Param("createDate") String createDate);

    /**
     * 获取当日所有签到记录
     * @param createDate
     * @return
     */
    @Select("select * from attendance where DATE_FORMAT(create_date,'%Y-%m-%d')=#{createDate}")
    public List<Attendance> findAttendanceList(@Param("createDate") String createDate);

    /**
     * 创建打卡记录
     * @param employee
     * @param createDate
     */
    @Insert("insert into attendance(employee,create_date) values(#{employee},#{createDate}) ")
    public void createAttendance(@Param("employee") int employee,@Param("createDate") Date createDate);

    /**
     * 签到
     * @param signIn
     * @param employee
     * @param createDate
     */
    @Update("update attendance set sign_in=#{signIn} where employee=#{employee} and DATE_FORMAT(create_date,'%Y-%m-%d')=#{createDate}")
    public void signIn(@Param("signIn") Date signIn,@Param("employee") Integer employee,@Param("createDate") String createDate);

    /**
     * 签退
     * @param signOut
     * @param employee
     * @param createDate
     */
    @Update("update attendance set sign_out=#{signOut} where employee=#{employee} and DATE_FORMAT(create_date,'%Y-%m-%d')=#{createDate}")
    public void signOut(@Param("signOut") Date signOut,@Param("employee") Integer employee,@Param("createDate") String createDate);

    /**
     * 设置status
     * @param status
     * @param employee
     * @param createDate
     */
    @Update("update attendance set STATUS =#{status} where employee=#{employee} and DATE_FORMAT(create_date,'%Y-%m-%d')=#{createDate}")
    public void jduge(@Param("status") String status,@Param("employee") Integer employee,@Param("createDate") String createDate);

    /**
     * 考勤异常查询
     */
    @SelectProvider(type =AttendanceSql.class,method = "exceptionRecord")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "employee",column = "employee"),
            @Result(property = "user", column = "employee", one = @One(select = "com.oa.hyperion.repository.UserMapper.getUser"))
    })
    public List<Attendance> exceptionRecord(Attendance attendance);

    /**
     * 查询当日是否休假
     */
    @Select("SELECT d.* FROM " +
            "controller c, template t,detail d,outline o,controller_template_merge ct " +
            "WHERE t.id=ct.template AND ct.id=d.controller_template AND d.outline=o.id AND c.id=ct.controller " +
            "AND d.content=#{date} and o.applicant=#{employee}")
    public Detail isVacation(@Param("date") String date,@Param("employee") int employee);


    public static class AttendanceSql{
        public String exceptionRecord(Attendance attendance){
            SQL sql=new SQL();
            sql.SELECT("*");
            sql.FROM("attendance");
            sql.WHERE("STATUS is not null");
            if(attendance.getEmployee()!=null&&attendance.getEmployee()>0){
                sql.WHERE("employee = #{employee}");
            }
            if (attendance.getCreateDate()!=null){
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                String creatDate=format.format(attendance.getCreateDate());
                sql.WHERE("DATE_FORMAT(create_date,'%Y-%m-%d')="+"'"+creatDate+"'");
            }
            if(StringIsEmpty.notEmpty(attendance.getStatus())){
                if(attendance.getStatus().equals("xz")){
                    sql.WHERE("STATUS in ('x','z')");

                }else {
                    sql.WHERE("STATUS=#{status}");
                }

            }
            System.out.println(sql.toString());
            return sql.toString();
        }
    }
}
